// subpages1/staffindex/staffindex.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    stafflogin:false,
    account:"",
    password:"",
    showImgjump:false,
    showImgjump2:false,
    showImgjumpBacon:false,
    showImgjumpPig :false,
    money:"",
    moneyticket:"",
    moneybacon:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;
    try {
      var value = wx.getStorageSync('shoptoken')
      if (value) {
        that.setData({
          stafflogin:true
        });
        app.setNavtitle('核销');
      }
    } catch (e) {
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  bntHexiaoMeet(){
    wx.scanCode({
      onlyFromCamera: true,
      scanType:['qrCode'],
      success (res) {
        console.log(res)
        var value = wx.getStorageSync('shoptoken');

        var data = {};
        data.shoptoken = value.token;
        data.os_id = value.os_id;
        data.map = 'applet_order_passwd_verify';
        data.type = 'meet';
        data.code = res.result;
        wx.request({
          url: app.globalData.requestUrl,
          data: data,
          success: function (res) {
            if (res.data.ec == 200) {
              console.log(res.data);
              wx.showModal({
                title: '提示',
                content: "核销成功",
                showCancel: false
              });
            } else {
              wx.showModal({
                title: '提示',
                content: res.data.em,
                showCancel: false
              });
            }
          },
          complete: function () {
            wx.hideLoading();
          }
        });
      }
    })
  },
  bntHexiao(){
    wx.scanCode({
      onlyFromCamera: true,
      scanType:['qrCode'],
      success (res) {
        console.log(res)
        var value = wx.getStorageSync('shoptoken');

        var data = {};
        data.shoptoken = value.token;
        data.os_id = value.os_id;
        data.map = 'applet_order_passwd_verify';
        data.type = 'order';
        data.code = res.result;
        wx.request({
          url: app.globalData.requestUrl,
          data: data,
          success: function (res) {
            if (res.data.ec == 200) {
              console.log(res.data);
              wx.showModal({
                title: '提示',
                content: "核销成功",
                showCancel: false
              });
            } else {
              wx.showModal({
                title: '提示',
                content: res.data.em,
                showCancel: false
              });
            }
          },
          complete: function () {
            wx.hideLoading();
          }
        });
      }
    })
  },
  bntList(){
    wx.navigateTo({
      url: '/subpages1/usedorderlist/usedorderlist?title=' + '核销订单'
    })
  },
  bntListMeet(){
    wx.navigateTo({
      url: '/subpages1/meetlist/meetlist?title=' + '核销礼品'
    })
  },
  inputChange:function(e){
    var that = this;
    var type = e.currentTarget.dataset.type;
    that.setData({
      [type]:e.detail.value
    })
  },
  login(){
    var that = this;
    var data = {};
    data.map = 'applet_order_shop_login';
    data.account = that.data.account ? that.data.account:'';
    data.password = that.data.password ? that.data.password:'';
    if (!data.account) {
      app.errorTip(that, '请输入账号', 2000);
      return;
    }
    if (!data.password) {
      app.errorTip(that, '请输入密码', 2000);
      return;
    }
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        if (res.data.ec == 200) {
          console.log(res.data);
          wx.setStorage({
            key: 'shoptoken',
            data: res.data.data,
            success: function (res) {
              that.setData({
                stafflogin:true
              });
            }
          });
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.em,
            showCancel: false
          });
        }
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
  btnExit(){
    var that = this;
    try {
      wx.removeStorageSync('shoptoken')
      that.setData({
        stafflogin:false
      });
      app.setNavtitle('登录');

    } catch (e) {
      // Do something when catch error
    }
  },
  bntMeat(){
    this.setData({
      showImgjump:true,
      money:"",
    });
  },
  bntMeatPay(){
    var that = this;
    if(this.data.money=='')
      return;
    wx.scanCode({
      onlyFromCamera: true,
      scanType:['qrCode'],
      success (res) {
        console.log(res)
        var value = wx.getStorageSync('shoptoken');
        var data = {};
        data.shoptoken = value.token;
        data.os_id = value.os_id;
        data.map = 'applet_order_passwd_verify';
        data.type = 'meat';
        data.code = res.result;
        data.money = that.data.money;
        wx.request({
          url: app.globalData.requestUrl,
          data: data,
          success: function (res) {
            if (res.data.ec == 200) {
              console.log(res.data);
              wx.showModal({
                title: '提示',
                content: "收款成功",
                showCancel: false
              });
              that.setData({
                showImgjump:false,
                money:"",
              });
            } else {
              wx.showModal({
                title: '提示',
                content: res.data.em,
                showCancel: false
              });
            }
          },
          complete: function () {
            wx.hideLoading();
          }
        });
      }
    })
  },
  bntMeatList(){
    wx.navigateTo({
      url: '/subpages1/meatlist/meatlist?title=' + '收款订单'
    })
  },
  btnClose(){
    this.setData({
      showImgjump:false
    });
  },
  bntMeatTicket(){
    this.setData({
      showImgjump2:true,
      moneyticket:"",
    });
  },
  bntMeatTicketPay(){
    var that = this;
    if(this.data.moneyticket=='')
      return;
    wx.scanCode({
      onlyFromCamera: true,
      scanType:['qrCode'],
      success (res) {
        console.log(res)
        var value = wx.getStorageSync('shoptoken');
        var data = {};
        data.shoptoken = value.token;
        data.os_id = value.os_id;
        data.map = 'applet_order_passwd_verify';
        data.type = 'meatticket';
        data.code = res.result;
        data.money = that.data.moneyticket;
        wx.request({
          url: app.globalData.requestUrl,
          data: data,
          success: function (res) {
            if (res.data.ec == 200) {
              console.log(res.data);
              wx.showModal({
                title: '提示',
                content: "收款成功",
                showCancel: false
              });
              that.setData({
                showImgjump2:false,
                moneyticket:"",
              });
            } else {
              wx.showModal({
                title: '提示',
                content: res.data.em,
                showCancel: false
              });
            }
          },
          complete: function () {
            wx.hideLoading();
          }
        });
      }
    })
  },
  bntMeatTicketList(){
    wx.navigateTo({
      url: '/subpages1/meatticketlist/meatticketlist?title=' + '核销肉券订单'
    })
  },
  btnCloseTicket(){
    this.setData({
      showImgjump2:false
    });
  },
  bntMeatBacon(){
    this.setData({
      showImgjumpBacon:true,
      money:"",
    });
  },
  bntMeatBaconList(){
    wx.navigateTo({
      url: '/subpages1/meatbaconlist/meatbaconlist?title=' + '收款订单'
    })
  },
  bntMeatBaconPay(){
    var that = this;
    if(this.data.moneybacon=='')
      return;
    wx.scanCode({
      onlyFromCamera: true,
      scanType:['qrCode'],
      success (res) {
        console.log(res)
        var value = wx.getStorageSync('shoptoken');
        var data = {};
        data.shoptoken = value.token;
        data.os_id = value.os_id;
        data.map = 'applet_order_passwd_verify';
        data.type = 'meatbacon';
        data.code = res.result;
        data.money = that.data.moneybacon;
        wx.request({
          url: app.globalData.requestUrl,
          data: data,
          success: function (res) {
            if (res.data.ec == 200) {
              console.log(res.data);
              wx.showModal({
                title: '提示',
                content: "收款成功",
                showCancel: false
              });
              that.setData({
                showImgjumpBacon:false,
                moneybacon:"",
              });
            } else {
              wx.showModal({
                title: '提示',
                content: res.data.em,
                showCancel: false
              });
            }
          },
          complete: function () {
            wx.hideLoading();
          }
        });
      }
    })
  },
  btnCloseBacon(){
    this.setData({
      showImgjumpBacon:false
    });
  },
  bntMeatPig(){
    this.setData({
      showImgjumpPig:true,
      money:"",
    });
  },
  bntMeatPigList(){
    wx.navigateTo({
      url: '/subpages1/meatpiglist/meatpiglist?title=' + '收款订单'
    })
  },
  bntMeatPigPay(){
    var that = this;
    if(this.data.moneypig=='')
      return;
    wx.scanCode({
      onlyFromCamera: true,
      scanType:['qrCode'],
      success (res) {
        console.log(res)
        var value = wx.getStorageSync('shoptoken');
        var data = {};
        data.shoptoken = value.token;
        data.os_id = value.os_id;
        data.map = 'applet_order_passwd_verify';
        data.type = 'meatpig';
        data.code = res.result;
        data.money = that.data.moneypig;
        wx.request({
          url: app.globalData.requestUrl,
          data: data,
          success: function (res) {
            if (res.data.ec == 200) {
              console.log(res.data);
              wx.showModal({
                title: '提示',
                content: "收款成功",
                showCancel: false
              });
              that.setData({
                showImgjumpPig:false,
                moneypig:"",
              });
            } else {
              wx.showModal({
                title: '提示',
                content: res.data.em,
                showCancel: false
              });
            }
          },
          complete: function () {
            wx.hideLoading();
          }
        });
      }
    })
  },
  btnClosePig(){
    this.setData({
      showImgjumpPig:false
    });
  },
})